import type {ActionType, ProColumns} from '@ant-design/pro-components';
import {ProTable} from '@ant-design/pro-components';
import {useRef, useState} from 'react';
import {merchantList, rewardList, updateReward} from "@/services/bz/api";
import {useRequest} from "@@/plugin-request/request";
import {Card, message} from "antd";
import {ProForm, ProFormText} from "@ant-design/pro-form/es";

type GithubIssueItem = {
    configCode: string;
    bucket: string;
    target: string;
};

export default () => {
    const actionRef = useRef<ActionType>();
    const [merchantData, setMerchantData] = useState([]);

    useRequest(merchantList, {
        onSuccess: (res) => {
            let data = {};
            //将拿到的返回值遍历
            res.list.map(item => {
                //使用接口返回值的id做为 代替原本的0，1
                data[item.merchantCode] = {
                    //使用接口返回值中的overdueValue属性作为原本的text:后面的值
                    text: item.merchantCode,
                };
            });
            setMerchantData(data);
        },
    });
    const columns: ProColumns<GithubIssueItem>[] = [
        {
            title: 'CID',
            dataIndex: 'cid',
            key: 'cid',
        },
        {
            title: '姓名',
            key: 'name',
            dataIndex: 'name',
        },
        {
            title: '绑定Model',
            key: 'bingModel',
            dataIndex: 'bingModel',
        },
        {
            title: '最近一次在线时间',
            key: 'lastTime',
            dataIndex: 'lastTime',
        },
        {
            title: '创建时间',
            key: 'createTime',
            dataIndex: 'createTime',
        },
        {
            title: '操作',
            valueType: 'option',
            key: 'option',
            render: (text, record, _, action) => [
                <a
                    key="editable"
                    onClick={() => {
                    }}
                >
                    绑定Model
                </a>,
                <a
                    key="editable"
                    onClick={() => {
                    }}
                >
                    查看密码
                </a>,
                <a
                    key="editable"
                    onClick={() => {
                    }}
                >
                    重置密码
                </a>
            ],
        },
    ];

    return (
        <div>
            <Card style={{marginBottom: "20px"}}>
                <ProForm<{
                    name: string;
                }>
                    onFinish={async (values) => {
                        // await waitTime(2000);
                        console.log(values);
                        message.success('提交成功');
                    }}
                    // 配置按钮的属性
                    submitter={{
                        resetButtonProps: {
                            style: {
                                display: 'none'
                            }
                        }
                    }

                    }
                >
                    <ProForm.Group>
                        <ProFormText
                            width="md"
                            name="name"
                            label="姓名"
                            placeholder="Please enter"
                        />
                    </ProForm.Group>

                </ProForm>
            </Card>
            <ProTable<GithubIssueItem>
                columns={columns}
                actionRef={actionRef}
                cardBordered
                request={async (params: any) => {
                    console.log("====", params)
                    let request = {
                        clientCode: null,
                        bucket: null
                    };
                    if (params.clientCode != undefined && params.clientCode != "") {
                        request.clientCode = params.clientCode
                    }
                    if (params.bucket != undefined && params.bucket != "") {
                        request.bucket = params.bucket
                    }
                    let response = await rewardList(request.clientCode, request.bucket);
                    return {data: response.data == null ? [] : response.data};
                }}
                editable={{
                    type: 'multiple',
                    onSave(params: any, value: any) {
                        updateReward(value.clientCode, value.bucket, value.targetFinishValue, value.target).then(() => {
                            message.success("修改成功")
                            actionRef.current?.reload()
                        })
                    }
                }}
                columnsState={{
                    persistenceKey: 'pro-table-singe-demos',
                    persistenceType: 'localStorage',
                    onChange(value) {
                        console.log('value: ', value);
                    },
                }}
                rowKey="id"
                search={false}
                form={{
                    // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
                    syncToUrl: (values, type) => {
                        console.log("~~~~")
                        if (type === 'get') {
                            return {
                                ...values,
                                created_at: [values.startTime, values.endTime],
                            };
                        }
                        return values;
                    },
                }}
                dateFormatter="string"
                options={false}
                revalidateOnFocus={false}

                pagination={{
                    showSizeChanger: true,
                }}
            />
        </div>


    );
};
